package com.dongjiantong.sys.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.sys.mapper.AnimalTypeMapper;
import com.dongjiantong.sys.model.AnimalType;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cao
 * @create 2017-11-15 下午4:36
 **/

@Slf4j
@Service(version = "1.0.3")
public class AnimalTypeServiceImpl extends ServiceMybatis<AnimalType> implements AnimalTypeService {

    @Resource
    private AnimalTypeMapper animalTypeMapper;

    @Override
    public AnimalType getAnimalByType(Integer animalCode)
    {
        Example example = new Example(AnimalType.class);
        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("animalCode",animalCode);

        AnimalType type =  this.selectOne(example);

        if(type==null)
        {
            type = new AnimalType();
        }
        return  type;
    }

    @Override
    public List<AnimalType> queryTreeAnimalType(Long pid,Long regionAccountId)
    {
        Example example = new Example(AnimalType.class);
        Example.Criteria criteria= example.createCriteria();

        //取系统设置的值
        if(regionAccountId==0)
        {
            criteria.andEqualTo("issys", 0);
        }
        else
        {
            //自定义的数据，一级不能自定义
            if(pid>0) {
                criteria.andEqualTo("issys", 1);
            }
        }


        //这里给出的条件查询为parentId=100
        criteria.andEqualTo("parentId", pid);
        criteria.andEqualTo("status", 0);
        return animalTypeMapper.selectByExample(example);
    }

    @Override
    public List<AnimalType> queryTreeNode(Long pid)
    {
        Example example = new Example(AnimalType.class);
        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("parentId", pid);
        criteria.andEqualTo("status",0);

        return animalTypeMapper.selectByExample(example);
    }

    @Override
    public List<AnimalType> queryOneLevel()
    {
        Example example = new Example(AnimalType.class);
        Example.Criteria criteria= example.createCriteria();
        criteria.andEqualTo("parentId", 0L);
        criteria.andEqualTo("status",0);

        return animalTypeMapper.selectByExample(example);
    }

    @Override
    public List<AnimalType> queryTwoLevel(Long pid,Long regionAccountId)
    {
        return  queryLevel(pid,regionAccountId);
    }

    @Override
    public List<AnimalType> queryThreeLevel(Long pid,Long regionAccountId)
    {
       return  queryLevel(pid,regionAccountId);
    }

    public List<AnimalType> queryLevel(Long pid,Long regionAccountId)
    {
        Example example = new Example(AnimalType.class);
        Example.Criteria criteria= example.createCriteria();

        criteria.andEqualTo("parentId", pid);
        criteria.andEqualTo("status",0);

        if(pid>0 && regionAccountId>0) {
            criteria.andEqualTo("issys", 1);
        }


        return animalTypeMapper.selectByExample(example);
    }


    /**
     * 查询二级分类
     * @param pid
     * @return
     */
    @Override
    public List<AnimalType> queryTwoLevel(Long pid)
    {
        return  queryLevel(pid,0L);
    }

    /**
     * 查询三级分类
     * @param pid
     * @return
     */
    @Override
   public List<AnimalType> queryThreeLevel(Long pid)
    {
        return  queryLevel(pid,0L);
    }

    /**
     * 根据动物编号获取其下一级动物编号List
     * @param animalCode 父动物编号
     * @return
     */
    @Override
    public List<Integer> queryChildCode(String animalCode) {

        AnimalType animalType = new AnimalType();
        animalType.setAnimalCode(Integer.valueOf(animalCode));
        AnimalType animalTypeFather = animalTypeMapper.selectOne(animalType);

        Example example = new Example(AnimalType.class);
        example.createCriteria().andEqualTo("parentId",animalTypeFather.getId())
                                                .andEqualTo("status",0);
        List<AnimalType> animalTypeList = animalTypeMapper.selectByExample(example);

        if (animalTypeList.size() > 0){
            List<Integer> list = Lists.newArrayList();
            for (AnimalType animalTypeChild : animalTypeList){
                list.add(animalTypeChild.getAnimalCode());
            }
            return list;
        }
        return null;
    }
}
